package _interview100;

/**
 * 238. 除自身以外数组的乘积
 */
public class No238 {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;

        int[] preArr = new int[n];
        preArr[0] = 1;
        for (int i = 1; i < n; i++) {
            preArr[i] = preArr[i - 1] * nums[i - 1];
        }

        int[] sufArr = new int[n];
        sufArr[n - 1] = 1;
        for (int i = n - 2; i >= 0; i--) {
            sufArr[i] = sufArr[i + 1] * nums[i + 1];
        }

        for (int i = 0; i < n; i++) {
            nums[i] = preArr[i] * sufArr[i];
        }
        return nums;
    }
}
